package web.admin.cms;


import domain.cms.CmsPage;
import exceptions.DeleteException;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.facade.admin.CmsAdminService;

import java.util.List;

public final class SectionList extends Panel {

    @SpringBean
    public CmsAdminService cmsService;
    final CmsAdmin parent;
    SectionsLdmModel sectionsModel;

    public SectionList(String id, final CmsAdmin parent) {
        super(id);
        this.parent = parent;
        sectionsModel = new SectionsLdmModel();
        SectionListView sectionListView = new SectionListView("section", sectionsModel);
        add(sectionListView);
    }

    public SectionList(String id) {
        super(id);
        this.parent = null;
        sectionsModel = new SectionsLdmModel();
        SectionListView sectionListView = new SectionListView("section", sectionsModel);
        add(sectionListView);
    }

    @Override
    protected void detachModel() {
        super.detachModel();
        sectionsModel.detach();
    }

    class SectionsLdmModel extends LoadableDetachableModel<List<CmsPage>> {

        @Override
        protected List<CmsPage> load() {
            return cmsService.getSections();
        }
    }

    class SectionListView extends ListView<CmsPage> {

        public SectionListView(String id, IModel<? extends List<CmsPage>> model) {
            super(id, model);
        }

        @Override
        protected void populateItem(final ListItem<CmsPage> item) {
            initStuffAndShit(item);
            AbstractReadOnlyModel<List<CmsPage>> model = new AbstractReadOnlyModel<List<CmsPage>>() {

                @Override
                public List<CmsPage> getObject() {
                    return item.getModelObject().getChilds();
                }
            };
            CmsCategoryListView cmsCategoryListView = new CmsCategoryListView("category", model);
            item.add(cmsCategoryListView);
        }
    }

    class CmsCategoryListView extends ListView<CmsPage> {


        private CmsCategoryListView(String string, IModel<List<CmsPage>> imodel) {
            super(string, imodel);
        }

        @Override
        protected void populateItem(final ListItem<CmsPage> item) {
            initStuffAndShit(item);

            AbstractReadOnlyModel<List<CmsPage>> model = new AbstractReadOnlyModel<List<CmsPage>>() {

                @Override
                public List<CmsPage> getObject() {
                    return item.getModelObject().getChilds();
                }
            };
            CmsPageListView cmsPageListView = new CmsPageListView("page", model);
            item.add(cmsPageListView);
        }

    }

    class CmsPageListView extends ListView<CmsPage> {

        public CmsPageListView(String string, IModel<? extends List<CmsPage>> imodel) {
            super(string, imodel);
        }

        @Override
        protected void populateItem(ListItem<CmsPage> item) {
            initStuffAndShit(item);
        }
    }

    private class DeleteLink extends Link {
        transient CmsPage item;

        private DeleteLink(String id, final ListItem<CmsPage> item) {
            super(id);
            this.item = item.getModel().getObject();
            item.detach();
        }

        @Override
        public void onClick() {
            try {
                cmsService.removePage(item);
                info(getLocalizer().getString("system.ok", this));
            } catch (DeleteException e) {
                error(getLocalizer().getString("page.cannot.delete", this));
                e.printStackTrace();
            } catch (Exception e) {
                error(getLocalizer().getString("system.error", this));
                e.printStackTrace();
            }
        }
    }

    private class AddLink extends Link {
        transient ListItem<CmsPage> item;

        private AddLink(String id, final ListItem<CmsPage> item) {
            super(id);
            this.item = item;
        }

        @Override
        public void onClick() {
            try {
                cmsService.addChild(item.getModelObject());
                info(getLocalizer().getString("system.ok", this));
            } catch (Exception e) {
                error(getLocalizer().getString("system.error", this));
                e.printStackTrace();
            }
        }
    }

    private void initStuffAndShit(final ListItem<CmsPage> item) {
        Link link = new Link("link", item.getModel()) {

            @Override
            public void onClick() {
                parent.replacePanelPage(getModel());
            }
        };
        link.add(new Label("title", new Model<String>(item.getModelObject().getTitle())));
        item.add(new Label("url", new Model<String>(item.getModelObject().getUrl())));

        item.add(link);
        item.add(new DeleteLink("delete", item));
        if ((item.getModelObject()).isPage()) {
            item.add(new AddLink("add", item).setVisible(false));
        } else {
            item.add(new AddLink("add", item));
        }


    }

}
